package com.chen.blue.converter.system;


import com.chen.blue.domain.dto.system.UserDto;

import com.chen.blue.domain.dto.system.update.UserDtoEdit;
import com.chen.blue.domain.model.system.UsersModel;

import com.chen.blue.domain.vo.system.UserVo;
import org.springframework.cglib.beans.BeanCopier;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created By CLT On 2024/4/27.
 */
@SuppressWarnings("all")
public abstract class UserConverter {

    private static final BeanCopier modelToVo = BeanCopier.create(UsersModel.class, UserVo.class, false);
    private static final BeanCopier dtoToModel = BeanCopier.create(UserDto.class, UsersModel.class, false);
    private static final BeanCopier dtoEditToModel = BeanCopier.create(UserDtoEdit.class, UsersModel.class, false);

    /**
     * model->vo
     *
     * @param model
     * @return
     */
    public static UserVo modelToVo(UsersModel model) {
        if (Objects.isNull(model)) {
            return null;
        }
        UserVo userVo = new UserVo();
        modelToVo.copy(model, userVo, null);
        return userVo;
    }

    /**
     * model->volist
     *
     * @param modelList
     * @return
     */
    public static List<UserVo> modelToVoList(List<UsersModel> modelList) {
        if (modelList.size() == 0) {
            return null;
        }

        List<UserVo> userVoList = new ArrayList<>();
        for (UsersModel usersModel : modelList) {
            userVoList.add(modelToVo(usersModel));
        }

        return userVoList;

    }

    /**
     * dto->model
     *
     * @param userDto
     * @return
     */
    public static UsersModel dtoToModel(UserDto userDto) {

        if (Objects.isNull(userDto)) {
            return null;
        }

        UsersModel usersModel = new UsersModel();
        dtoToModel.copy(userDto, usersModel, null);
        return usersModel;

    }

    /**
     * editDto->model
     *
     * @param userDtoEdit
     * @return
     */
    public static UsersModel dtoEditToModel(UserDtoEdit userDtoEdit) {

        if (Objects.isNull(userDtoEdit)) {
            return null;
        }

        UsersModel usersModel = new UsersModel();
        dtoEditToModel.copy(userDtoEdit, usersModel, null);
        return usersModel;

    }

}
